package com.wangwenjun.concurrency.chapter17;


/**
 *
 * 3个线程轮流打印123各10次,使用wait和notify
 *
 */
public class TaoBaoTest11 {

    private static int flag = 1;

    public static void main(String[] args) throws InterruptedException {

        Object lock = new Object();

        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                synchronized (lock){
                    while (flag != 3){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("3");
                    lock.notifyAll();
                    flag = 1;
                }
            }
        }, "t3").start();

        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                synchronized (lock){
                    while (flag != 2){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("2");
                    lock.notifyAll();
                    flag = 3;
                }
            }
        }, "t2").start();

        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                synchronized (lock){
                    while (flag != 1){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("1");
                    lock.notifyAll();
                    flag = 2;
                }
            }
        }, "t1").start();




    }









}
